<?php
/**
 * Description: 支付回调通知处理
 * Author: hezhizheng
 * Date: 2018/11/28
 * Time: 10:47
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Api\V1\App\Order;


use App\Exceptions\ApiErrCode;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Extension\Extension;
use App\Services\Omnipay\AliPay;
use App\Services\Omnipay\Paypal;
use App\Services\Omnipay\WechatPay;
use Illuminate\Http\Request;

class PayNotifyController extends Controller
{
    const ROUTE_PAY_NOTIFY_GATEWAY_NAME = 'app.pay.gateway.notify';
    const ROUTE_PAY_PAL_REDIRECT_URL = 'app.pay.pal.redirect.url';

    /**
     * 支付回调
     *
     * @param $gateway
     * @return bool
     * @throws \Exception
     */
    public function payCallbackAction($gateway)
    {
        $result = $this->callBackForGateway($gateway);

        return $result;
    }

    /**
     * pay_pal 重定向空白页，网址带上code参数，App拦截获取参数。
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function payPalRedirectUrl(Request $request)
    {
        $code = $request->code;

        return view('paypal.confirm', compact('code'));
    }

    /**
     * @param $gateway
     * @return bool
     * @throws \Exception
     */
    private function callBackForGateway($gateway)
    {
        switch ($gateway) {
            case Extension::CODE_PAYMENT_ALI:
                return AliPay::aliNotify();
                break;
            case Extension::CODE_PAYMENT_WECHAT:
                return WechatPay::wechatNotify();
                break;
            case Extension::CODE_PAYMENT_PAYPAL:
                return (new Paypal)->payPalNotify();
                break;
            default:
                throw new ApiException(ApiErrCode::CUSTOMIZE_ERROR, 'not support pay extension');
                break;
        }
    }

}